package com.ming.springcloud.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.ming.springcloud.consumer.service.BookRibbonService;
import com.ming.springcloud.consumer.util.ResponseEntity;
import com.ming.springcloud.consumer.vo.Books;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/book_ribbon")
public class BookRibbonController {
    @Resource
    private BookRibbonService bookRibbonService;


    @RequestMapping(value = "/findAllBooks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity findAllBook(){
        String userJSON = bookRibbonService.findBooks();
        //   System.out.println(userJSON);
        return ResponseEntity.success().add("pageInfo",userJSON);
    }

    @RequestMapping(value = "/findBooksById/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity findStudentsByIds(@PathVariable("id")Integer id){
        //    System.out.println("id="+id);
        String userJSON = bookRibbonService.findBooksById( id);
        System.out.println(userJSON);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);

        //获取回传的josn数据
        String bookId=object.get("bookId").toString();
        String bookName=object.get("bookName").toString();
        String publishing=object.get("publishing").toString();
        String serviceName=object.get("serviceName").toString();
        String author=object.get("author").toString();
        String recommend=object.get("recommend").toString();
        String price=object.get("price").toString();
        String ids=object.get("id").toString();

        Books book=new Books();
        book.setId(Integer.parseInt(ids));
        book.setServiceName(serviceName);
        book.setAuthor(author);
        book.setBookId(bookId);
        book.setBookName(bookName);
        book.setPrice(Integer.parseInt(price));
        book.setPublishing(publishing);
        book.setRecommend(recommend);

           System.out.println("学生信息="+book);
        return  ResponseEntity.success().add("book",book);
    }
}
